package org.lionel.zk;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ZkTest implements Runnable {
    private static final Lock lock = new ReentrantLock();
    private static int inventory = 1;
    private static final int NUM = 10;
    private static CountDownLatch cdl = new CountDownLatch(NUM);

    public static void main(String[] args) {
        for (int i = 0; i <= NUM; i++) {
            new Thread(new ZkTest()).start();
            cdl.countDown();
        }
    }

    @Override
    public void run() {
       lock.lock();
        try {
            cdl.await();
            if (inventory > 0) {
                Thread.sleep(5);
                inventory--;
            }
            System.out.println(inventory);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
